package com.lingying.caiwudailikehudaun.controller.power;


import com.alibaba.fastjson.JSONObject;
import com.lingying.caiwudailikehudaun.domain.ceiWuDaiLiUserBean.CaiWuDaiLiUserBean;
import com.lingying.caiwudailikehudaun.dto.PowerDTO;
import com.lingying.caiwudailikehudaun.mapper.power.PowerMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/power")
public class PowerController {
    @Resource
    PowerMapper powerMapper;

    @Resource
    HttpServletRequest httpServletRequest;

    /**
     * 封装菜单集合
     * @param rootMenu 角色对应所有菜单
     * @return
     */
    private List<PowerDTO> diGuiMenu(List<PowerDTO> rootMenu) {
        //rootMenu所有菜单的集合
        List<PowerDTO> menuList = new ArrayList<PowerDTO>();
        // 先找到所有的一级菜单
        for (int i = 0; i < rootMenu.size(); i++) {
            // 一级菜单没有fId
            if (rootMenu.get(i).getFId()==0) {
                menuList.add(rootMenu.get(i));
            }
        }
        // 为一级菜单设置子菜单，getChild是递归调用的
        for (PowerDTO menu : menuList) {
            List<PowerDTO> childs = getChild(menu.getKeHuDuanYeMianLuJingId().toString(), rootMenu);
            if (childs != null && childs.size() > 0) {
                menu.setCList(childs);
            }
        }
        return menuList;
    }
    private List<PowerDTO> getChild(String id, List<PowerDTO> rootMenu) {
        // 子菜单
        List<PowerDTO> childList = new ArrayList<PowerDTO>();
        for (PowerDTO menu : rootMenu) {
            // 遍历所有节点，将父菜单id与传过来的id比较
            if (menu.getFId()!=0) {
                if ((menu.getFId() + "").equals(id)) {
                    childList.add(menu);
                }
            }
        }
        // 把子菜单的子菜单再循环一遍
        for (PowerDTO menu : childList) {
            // 没有url子菜单还有子菜单
            if (menu.getFId()!=0) {
                // 递归
                List<PowerDTO> childs = getChild(menu.getKeHuDuanYeMianLuJingId().toString(), rootMenu);
                if (childs != null && childs.size() > 0) {
                    menu.setCList(childs);
                }
            }
        } // 递归退出条件
        if (childList.size() == 0) {
            return null;
        }
        return childList;
    }


    //退出登录清空后台session
    @RequestMapping("/logout")
    public String tologout(HttpServletRequest request){
        request.getSession().invalidate();
        return "0";
    }

    /**
     * 根据当前登录人的userId查询caiWuDaiLiUser
     * 表中所属的所有公司名称，并回显到前端
     * 桑满  20210427
     * @return
     */
    @GetMapping("/queryCompanyName")
    public Object queryCompanyName(){
        JSONObject object=new JSONObject();
        String phone = (String) httpServletRequest.getSession().getAttribute("phone");
//        System.err.println("我获取到的是userId是==》"+userId);
        if(phone!=null){
            List<CaiWuDaiLiUserBean> caiWuDaiLiUserList=powerMapper.queryCompanyName(phone);
//            System.err.println("userId是==============》"+userId);
            if(caiWuDaiLiUserList!=null){
                object.put("code",0);
                object.put("msg","查询成功");
                object.put("data",caiWuDaiLiUserList);
            }else{
                object.put("code",500);
                object.put("msg","查询失败");
            }
            return object;
        }else{
            return null;
        }
    }

    /**
     * 根据companyName查询做账申报表中的zuoZhangShenBaoId
     * 桑满  20210429
     * @param companyName
     * @return
     */
    @GetMapping("/queryZuoZhangShenBaoId")
    public Object queryZuoZhangShenBaoId(Integer zuoZhangShenBaoId,String companyName,Integer daiLiShangCid){
        System.err.println("daiLiShangCid===>"+zuoZhangShenBaoId);
        System.err.println("daiLiShangCid===>"+companyName);
        System.err.println("daiLiShangCid===>"+daiLiShangCid);
        JSONObject object=new JSONObject();
        httpServletRequest.getSession().setAttribute("zuoZhangShenBaoId",zuoZhangShenBaoId);
        httpServletRequest.getSession().setAttribute("companyName",companyName);
        httpServletRequest.getSession().setAttribute("daiLiShangCid",daiLiShangCid);

        Integer waiBuRenYuanId = (Integer) httpServletRequest.getSession().getAttribute("userId");
        Integer jiGouId = (Integer) httpServletRequest.getSession().getAttribute("jiGouId");
        String phone = (String) httpServletRequest.getSession().getAttribute("phone");

        //先去查询到当前
        Integer jueSeId = powerMapper.queryRoleYeMian(waiBuRenYuanId,zuoZhangShenBaoId,phone,jiGouId);

        //根据当前角色id去keHuDuanJueSeYeMian表中拿到所拥有的页面
        List<PowerDTO> keHuDuanJueSeYeMianList = powerMapper.queryJueSeYeMian(jiGouId,jueSeId);
        List<PowerDTO> list= diGuiMenu(keHuDuanJueSeYeMianList);
        if(list.size() > 0){
            object.put("code",0);
            object.put("msg","查询成功");
            object.put("data",list);
        }else{
            object.put("code",500);
            object.put("msg","查询失败");
        }
        return object;
    }
}



















